<template>
  <div class="article-comments">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      @load="getComments"
      :immediate-check="this.type ? true : false"
    >
      <van-cell
        class="comment-info"
        v-for="comment in comments"
        :key="comment.com_id"
      >
        <template #icon>
          <van-image
            :src="comment.aut_photo"
            round
            fit="cover"
            class="avatar"
          />
        </template>
        <template #title>
          <div class="user-name">{{ comment.aut_name }}</div>
          <div class="content">{{ comment.content }}</div>
          <div class="desc">
            <span class="pubdate">{{ label(comment) }}</span>
            <van-button round size="small" @click="replyComment(comment)">
              回复 {{ comment.reply_count }}
            </van-button>
          </div>
        </template>
        <div><van-icon name="good-job-o" />赞</div>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { getCommentsAPI } from '@/api'
import dayjs from '@/utils/dayjs'
export default {
  name: '',
  props: {
    newComment: Object,
    source: String,
    type: String
  },
  data() {
    return {
      loading: false,
      finished: false,
      comments: [],
      offset: null,
      error: false
    }
  },
  computed: {
    label() {
      return function (comment) {
        return dayjs(comment.pubdate).fromNow()
      }
    }
  },
  watch: {
    newComment: {
      deep: true,
      handler() {
        this.comments.unshift(this.newComment)
      }
    }
  },
  created() {},
  methods: {
    // 获取评论
    async getComments() {
      try {
        const { data } = await getCommentsAPI({
          type: this.type || 'a',
          source: this.source || this.$route.query.articleId,
          offset: this.offset,
          limit: 10
        })
        const results = data.data.results
        this.comments.push(...results)
        this.offset = data.data.last_id
        if (this.offset === null) {
          this.finished = true
          return
        }
      } catch (error) {
        this.error = true
      } finally {
        this.loading = false
      }
    },
    replyComment(comment) {
      this.$emit('showReplyCommentPopup', comment)
    }
  }
}
</script>

<style scoped lang="less">
.article-comments {
  :deep(.van-list) {
    margin-bottom: 0.6rem;
  }
}
.comment-info {
  padding: 0.26667rem 0.42667rem;
  .avatar {
    width: 0.93333rem;
    height: 0.93333rem;
    margin-right: 0.22667rem;
  }
  .user-name {
    font-size: 0.37333rem;
    line-height: 0.64rem;
    color: #466b9d;
  }
  .content {
    margin-top: 0;
    margin-bottom: 0.42667rem;
    color: #363636;
    font-size: 0.32rem;
    line-height: 0.48rem;
  }
  .desc {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 4rem;
    margin-bottom: 0.42667rem;
    .pubdate {
      margin-right: 10px;
      font-size: 0.32rem;
      color: #969799;
    }
    .van-button {
      width: 2.2rem;
      height: 0.64rem;
      line-height: 0.64rem;
      font-size: 0.28rem;
      color: #222;
    }
  }
}
</style>
